package com.link.base.base.orgnization.controller;

import com.link.base.base.orgnization.model.Orgnization;
import com.link.base.base.orgnization.service.OrgImportService;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.model.CallParam;
import com.link.core.cllog.LCLogger;
import com.link.core.util.UserUtil;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.query.Filter;
import com.link.core.basic.service.BasicService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @comments 组织导入请求处理
 */
@Controller
@RequestMapping("/link/orgImport")
public class OrgnizationImportController extends BasicController<Orgnization> {
    @Resource
    private OrgImportService orgImportService;

    @Override
    public BasicService<Orgnization> getBasicService() throws Exception {
        return orgImportService;
    }

    @Override
    public void beforQueryAllOrExample(Orgnization entity, HttpServletRequest request) throws Exception {
        CoreUser user = UserUtil.getUser();
        if (entity.getConditionSql() == null) {
//          entity.setConditionSql("");
        }
//      entity.setConditionSql(entity.getConditionSql()+" and createdBy="+"'"+user.getId()+"'");
        Filter filter = new Filter("createdBy", String.valueOf(user.getId()));
        List<Filter> filterList = new ArrayList<Filter>();
        filterList.add(filter);
        entity.setFilterList(filterList);
    }

    @RequestMapping(value = "/check", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> check(com.link.core.basic.model.CallParam callParam,
                                     HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            orgImportService.runOrganizationCheck(callParam);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/orgImpInsert", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> orgImpInsert(CallParam callParam, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            orgImportService.runNewOrganizationForimp(callParam);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

}
